<!-- 数字键盘 -->
<template>
	<view  class="digit-keyboard">
		<view class="digit-keyboard_bg" @tap="hide"></view>
		<view class="digit-keyboard_area">
			<view class="input-area">
				<view class="item">{{label}}：</view>
				<view class="item">
					<input :placeholder="inputVal" v-model="val" class="input" disabled />
				</view>
				<view class="item">
					<button type="primary" size="mini" @tap="confirm">确定</button>
				</view>
			</view>
			<view class="number-area">
				<view class="item" @tap="modifyNum(1)"><text>1</text></view>
				<view class="item" @tap="modifyNum(2)">2</view>
				<view class="item" @tap="modifyNum(3)">3</view>
				<view class="item" @tap="modifyNum('del')">
					<icon type="cancel" size="20" />
				</view>
				<view class="item" @tap="modifyNum(4)">4</view>
				<view class="item" @tap="modifyNum(5)">5</view>
				<view class="item" @tap="modifyNum(6)">6</view>
				<view class="item" @tap="modifyNum('add')">加1</view>
				<view class="item" @tap="modifyNum(7)">7</view>
				<view class="item" @tap="modifyNum(8)">8</view>
				<view class="item" @tap="modifyNum(9)">9</view>
				<view class="item" @tap="modifyNum('minus')">减1</view>
				<view class="item" @tap="modifyNum('-')">-</view>
				<view class="item" @tap="modifyNum(0)">0</view>
				<view class="item" @tap="modifyNum('.')">.</view>
				<view class="item" @tap="modifyNum('clear')">清除</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import NP from '../../util/numberPrecision/numberPrecision.js'

	var _boundaryCheckingState = true

	export default {
		props: {
			inputVal: {
				type: [String],
				default: ''
			},
			label:{
				type:String,
				default:'现金'
			},
		},
		data() {
			return {
				val: ''
			};
		},
		created() {
	 
		},
		methods: {
			//隐藏
			hide(){
				this.$emit('cancel');
			},
			confirm(){
				let val =this.val;
				let valNew = val.slice(-1);
				if(valNew == '.'){
					val= val.slice(0,-1);
				}
				this.$emit('confirm',val);
			},
			modifyNum(sign) {
				let {
					val
				} = this;
				//删除
				if (sign == 'del') {
					if (val.length > 0) {
						let valNew = val.slice(0, -1);
						if (valNew.length == 0) {
							val = '';
						} else {
							val = valNew;
						}
					}
				} else if (sign == 'add') { //加1
					val = this.plus(Number(val), 1) + '';
				} else if (sign == 'minus') { //减1
					val = this.minus(Number(val), 1) + '';
				} else if (sign == 'clear') { //清除
					val = '';
				} else if (sign == '-') { //代表负数
					if (val.indexOf('-') == -1) {
						val = '-' + val;
					}
				} else if (sign == '.') { //点符号
					if (val.indexOf('.') == -1 && val.length > 0) {
						val = val + '.';
					}
				} else {
					if ((val == '0' && sign == '0') || (val == '-0' && sign == '0') || (val == '0' && sign != '.') || (val == '-0' && sign != '.')) {
						return;
					}
					val = val + sign;
				}
				//小数点大于3位不赋值
				let arr = val.split('.');
				if (arr.length == 2 && arr[1].length > 3) {
					return;
				}
				this.val = val;
			},
			plus(num1, num2) {
					var others = [];
					for (var _i = 2; _i < arguments.length; _i++) {
							others[_i - 2] = arguments[_i]
					}
					if (others.length > 0) {
							return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1)))
					}
					var baseNum = Math.pow(10, Math.max(this.digitLength(num1), this.digitLength(num2)))
					return (this.times(num1, baseNum) + this.times(num2, baseNum)) / baseNum
			},
			minus(num1, num2) {
					var others = [];
					for (var _i = 2; _i < arguments.length; _i++) {
							others[_i - 2] = arguments[_i]
					}
					if (others.length > 0) {
							return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1)))
					}
					var baseNum = Math.pow(10, Math.max(this.digitLength(num1), this.digitLength(num2)))
					return (this.times(num1, baseNum) - this.times(num2, baseNum)) / baseNum
			},
			times(num1, num2) {
					var others = [];
					for (var _i = 2; _i < arguments.length; _i++) {
							others[_i - 2] = arguments[_i];
					}
					if (others.length > 0) {
							return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
					}
					var num1Changed = this.float2Fixed(num1);
					var num2Changed = this.float2Fixed(num2);
					var baseNum = this.digitLength(num1) + this.digitLength(num2);
					var leftValue = num1Changed * num2Changed;
					this.checkBoundary(leftValue)
					return leftValue / Math.pow(10, baseNum);
			},
			float2Fixed(num) {
					if (num.toString().indexOf('e') === -1) {
							return Number(num.toString().replace('.', ''));
					}
					var dLen = this.digitLength(num);
					return dLen > 0 ? this.strip(num * Math.pow(10, dLen)) : num;
			},
			strip(num, precision) {
					if (precision === void 0) { precision = 12; }
					return +parseFloat(num.toPrecision(precision));
			},
			digitLength(num) {
					// Get digit length of e
					var eSplit = num.toString().split(/[eE]/)
					var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0))
					return len > 0 ? len : 0
			},
			checkBoundary(num) {
					if (_boundaryCheckingState) {
							if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
									console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
							}
					}
			}
		}
	}
</script>


<style lang="less" scoped>
	.digit-keyboard {
		position: fixed;
		left: 0;
		top: 0;
		z-index: 999;
		width: 100%;
		height: 100%;
	}

	.digit-keyboard_bg {
		width: 100%;
		height: 100%;
		background: #000;
	}

	.digit-keyboard_area {
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		background: #efefef;
		padding-bottom: 20rpx;
	}

	.input-area {
		display: flex;
		align-items: center;
		padding: 10rpx;
		background: #FFFFFF;

		.item {
			font-size: 28rpx;

			&:nth-of-type(2) {
				flex: 1 0 auto;
				padding-right: 10rpx;
			}

			&:nth-of-type(3) {
				font-size: 0;
			}
		}

		.input {
			background: #eeeeee;
			text-indent: 10rpx;
			font-size: 28rpx;
			height: 60rpx;
		}
	}

	.number-area {
		display: flex;
		justify-content: space-around;
		flex-wrap: wrap;
		text-align: center;

		.item {
			margin-top: 20rpx;
			flex: 0 0 22%;
			background: #FFFFFF;
			line-height: 80rpx;
			font-size: 30rpx;
			font-weight: bold;
		}
	}
</style>
